package com.coco.live.service.impl;

import com.coco.common.constant.LiveEnum;
import com.coco.common.constant.LoginEnum;
import com.coco.common.result.CommonResponse;
import com.coco.live.mapper.LiveMapper;
import com.coco.live.mapper.LiveProductMapper;
import com.coco.live.pojo.TbLive;

import com.coco.live.pojo.TbLiveProduct;
import com.coco.live.service.ILiveService;
import com.coco.live.vo.ItemVo;
import com.coco.user.mapper.UserMapper;
import com.coco.user.pojo.TbUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @date 2021/07/27 18:51
 * @version 1.0
 */
@Service
public class LiveServiceImpl implements ILiveService {

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private LiveProductMapper liveProductMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 直播列表
     * @param tbLive
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findPage(TbLive tbLive, int page, int size) {
        PageHelper.startPage(page,size);
        List<TbLive> list = liveMapper.findPage(tbLive);
        return new PageInfo(list);
    }

    /**
     * 修改直播状态
     * @param id
     * @param status
     */
    @Override
    public void update(Long id, String status) {
        liveMapper.update(id,status);
    }


    @Override
    public TbLive findOne(Integer id) {
        return liveMapper.findOne(id);
    }

    @Override
    public CommonResponse addLive(TbLive live,Long[] itmes,String token) {

        String phone = stringRedisTemplate.boundValueOps(token).get();
        //根据手机号查询当前登录用户信息
        TbUser user = userMapper.findUserByPhone(phone);
        //用户id
        live.setUid(user.getId().longValue());
        //用户名称
        live.setUsername(user.getUserName());
        //开始直播时间
        live.setStartTime(new Date());
        //默认直播中
        live.setStatus("0");
        //销售金额
        live.setSalary(new BigDecimal(0));
        //创建时间
        live.setCreateTime(new Date());
        live.setRole("1");
        //先添加直播表
        liveMapper.addLive(live);
        //添加直播商品表
        Arrays.asList(itmes).stream().forEach(item->{
            //根据商品id查询商品名称
            ItemVo it = liveMapper.findItem(item);
            TbLiveProduct liveProduct = new TbLiveProduct();
            liveProduct.setLid(live.getId());
            liveProduct.setItemId(item.longValue());
            liveProduct.setPrice(it.getPrice());
            //spu名称
            liveProduct.setTitle(it.getGname());
            liveProductMapper.insert(liveProduct);
        });
        return new CommonResponse(LiveEnum.CREATE_FAIL.getCode(), LiveEnum.CREATE_SUCCESS.getMessage(),live.getId());
    }




}
